(function($){
    let table=null;
    //默认配置
    let defaultOption = {
        css: {
            height: '90%',
            top: '2px',
            right: '-1px',
            backgroundColor: '#dbe5f7',
            width: '5px',
        },
        //下标从0开始
        disableCol: []
    };
    let sx, ex;
    let tdCount = 0;

    let $firstTr = null;
    let tdIndex = -1;
    let $tds;
    // 距离右边框像素
    // let ps=-2;
    $.fn.initResizeableTable=function(obj){
        let option = {};
        copyObject(defaultOption, option);
        if (typeof obj === 'object'){
           let css = obj.css;
           let sourceCss = option.css;
           copyObject(css, sourceCss);
           copyObject(obj, option);
           option.css = sourceCss;
           if (option.disableCol == null || option.disableCol == undefined){
               option.disableCol = [];
           }
        }
        table=this;
        let $firstTds =  $(table).find("tr:first").find("td,th");
        $firstTr =  $(table).find("tr:first");
        $tds = $firstTds;
        // $(table).find("tr:first").bind("mousemove", tdMousemove);
        $firstTr.bind("mouseup", tdMouseup);

        tdCount = $firstTds.length;
        // 给表头列加移动事件
        $firstTds.each(function(i){
            if (i != tdCount -1 && !option.disableCol.includes(i, 0)) {
                // 向每个TH内部追加一个分割条div,用来拖动
                $(this).append('<div id="line' + i + '" style="position:absolute;z-index:1;display:block;cursor:e-resize;opacity:0;filter:alpha(opacity=0);"></div>');
                $("#line" + i).css(defaultOption.css);
                $(this).css("position", "relative");
                $("#line" + i).bind("mousedown", mousedown);
            }
        });	// end each
    };// end initTable

     // line鼠标按下事件
     let mousedown=function(e){
        e = e||window.event ;
        sx = mousePosition(e).x;
        tdIndex = Number(this.id.substring("line".length));
        $firstTr.bind("mousemove", tdMousemove);
        $firstTr.css("cursor", "e-resize");
        return false;
    };

    let mousePosition = function(event){
        let pageX = event.pageX,pageY = event.pageY;
        if (pageX === undefined){//IE8及更早版本
            pageX = event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
        }
        if (pageY === undefined){
            pageY = event.clientY + (document.body.scrollTop || document.documentElement.scrollTop);
        }
        return {x: pageX, y: pageY};
    };

    let tdMousemove = function (e) {
        if (tdIndex != -1) {
            e = e || window.event;
            let position = mousePosition(e);
            ex = position.x;
            let $currentTd = $tds.eq(tdIndex);
            let $nextTd = $tds.eq(tdIndex + 1);
            let distance = ex - sx;
            if (distance < 0) {
                if ($currentTd.width() > 30 && -distance < $currentTd.width() - 30) {
                    $currentTd.width($currentTd.width() + distance);
                    $nextTd.width($nextTd.width() - distance);
                }
            } else if (distance > 0) {
                if ($nextTd.width() > 30 && distance < $nextTd.width() - 30) {
                    $currentTd.width($currentTd.width() + distance);
                    $nextTd.width($nextTd.width() - distance);
                }
            }
        }
        return false;
    };

    let tdMouseup = function (e) {
        $(this).unbind("mousemove");
        tdIndex = -1;
        $(this).css("cursor", "default");
        return false;
    };

    function copyObject(sourceSrc, distSrc){
        if (typeof sourceSrc === 'object' && typeof distSrc === 'object'){
            for (let key in sourceSrc){
                distSrc[key] = sourceSrc[key];
            }
        }
    }
})(jQuery);